#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

struct ListNode
{
    int val;
    struct ListNode* next;
};

typedef struct ListNode ListNode;

int ListSize(ListNode* phead)
{
    ListNode* pcur = phead;
    int size = 0;
    while (pcur != NULL)
    {
        size++;
        pcur = pcur->next;
    }
    return size;
}

int ListGetNum(ListNode* phead, int pos)
{
    assert(phead);
    ListNode* pcur = phead;
    while (pos--)
    {
        pcur = pcur->next;
    }
    return pcur->val;
}

ListNode* BuyNode(int x)
{
    ListNode* node = (ListNode*)malloc(sizeof(ListNode));
    node->val = x;
    node->next = NULL;
    return node;
}

void ListPushFront(ListNode** pphead, int x)
{
    ListNode* NewNode = BuyNode(x);
    NewNode->next = *pphead;
    *pphead = NewNode;
}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    ListNode* RetList = NULL;
    int arr1[101] = { 0 }, arr2[101] = { 0 }, i = 0, result[101] = { 0 }, max = 0;
    for (i = 0; i < ListSize(l1); i++)
    {
        arr1[i] = ListGetNum(l1, i);
    }
    for (i = 0; i < ListSize(l2); i++)
    {
        arr2[i] = ListGetNum(l2, i);
    }
    max = ListSize(l1) > ListSize(l2) ? ListSize(l1) : ListSize(l2);
    for (i = 0; i < max; i++)
    {
        result[i] += arr1[i] + arr2[i];
        result[i + 1] = result[i] / 10;
        result[i] %= 10;
    }
    if (result[max] != 0)
    {
        for (i = 0; i <= max; i++)
        {
            ListPushFront(&RetList, result[i]);
        }
    }
    else
    {
        for (i = 0; i < max; i++)
        {
            ListPushFront(&RetList, result[i]);
        }
    }
    return RetList;
}

void test()
{
    ListNode* plist1, *plist2, *RetList = NULL;
    plist1 = plist2 = NULL;
    ListPushFront(&plist1, 3);
    ListPushFront(&plist1, 4);
    ListPushFront(&plist1, 2);
    ListPushFront(&plist2, 4);
    ListPushFront(&plist2, 6);
    ListPushFront(&plist2, 5);
    RetList = addTwoNumbers(plist1, plist2);
}

int main()
{
    test();
    return 0;
}